﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Modules_yahoo_AddNew : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
        this.btnCancel.Attributes.Add("onclick","return ClearAll()");

        if (!this.IsPostBack) { 

        }
    }

    private Boolean CheckNick(String strNick,DamiWebDataContext db) {
        Boolean bBool = false;
        var query = from p in db.tbl_Nick_Yahoos
                    where p.Nickname == strNick
                    select p;
        if (query.Count<tbl_Nick_Yahoo>() > 0)
            bBool = true;
        return bBool;
    }

    private void AddNew(String strNickName,DamiWebDataContext db) {
        String strMess = "Nick name này đã tồn tại!";
        if (CheckNick(strNickName, db)){
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
        }
        else {
            try
            {
                WebData data = new WebData();
                DataLibrary.tbl_Nick_Yahoo item = new DataLibrary.tbl_Nick_Yahoo();
                item.Nickname = this.txtNickName.Text.Trim();
                item.IsActive = 1;
                item.OrderId = Convert.ToInt32(txtOrderId.Text.Trim());
                item.Description = txtDescription.Text.Trim();
                item.Phone = txtPhone.Text;
                data.tbl_Nick_Yahoos.InsertOnSubmit(item);
                strMess = Message.Insert;
            }
            catch (Exception err)
            {
                strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
            }
        }

        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);

    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        using (DamiWebDataContext db = new DamiWebDataContext()) {
            AddNew(txtNickName.Text.Trim(), db);
        }

    }
}